<template>
  <div>
    <el-button
      v-auth="{v: pms, bool: isOperation}"
      class="border-btn"
      size="small"
      style="margin-bottom: 20px;"
      @click="handlAdd"
    >新建联系人</el-button>
    <table-view
      :data="tableData"
      header-cell-class-name="table-header"
      size="medium"
      border
      :cell-style="cellStyle"
      @row-click="aloneClick"
    >
      <el-table-column
        type="index"
        label="序号"
        width="50"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        prop="linkmanName"
        label="联系人姓名"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        prop="linkmanPost"
        label="职位"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        prop="tel"
        label="联系电话"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        prop="updatedAt"
        label="创建时间"
        align="center"
        :show-overflow-tooltip="true"
      >
        <template #default="{row}">
          {{ dateFormat("yyyy-mm-dd HH:MM:SS",row.createdAt) }}
        </template>
      </el-table-column>
      <el-table-column
        v-if="isOperation"
        label="操作"
        align="center"
      >
        <template #default="{row}">
          <el-button
            v-auth="{v: pms}"
            type="text"
            @click="handlEdit(row)"
          >编辑</el-button>
          <el-button
            v-auth="{v: pms}"
            type="text"
            @click="handlDele(row.id)"
          >删除</el-button>
        </template>
      </el-table-column>
    </table-view>
    <Pagination
      v-model:page="queryParams.page"
      v-model:limit="queryParams.pageSize"
      style="display: flex; justify-content: flex-end"
      :total="total"
      @pagination="handlList"
    />
    <addContact
      ref="addContactDialog"
      :title="title"
      @succsess="succsess"
    />
  </div>
</template>

<script>
import {defineAsyncComponent, getCurrentInstance, inject, onMounted, reactive, toRefs,} from 'vue'
import {useRoute} from 'vue-router'

export default {
  name: 'Contact',
  components: {
    addContact: defineAsyncComponent(() => import('./addContact.vue')),
  },
  props: {
    typeData: {
      type: String,
      default() {
        return ''
      },
    },
    isOperation: {
      type: Boolean,
      default: true,
    },
    // 操作权限
    pms: {
      type: Array,
      default: () => {},
    },
  },
  setup(props, context) {
    const route = useRoute()
    const currentInstance = getCurrentInstance()
    const api = inject('api')
    var data = reactive({
      tableData: [{ tags: 'qwe' }],
      addContactDialog: null,
      queryParams: {
        relationId: Number(route.query.id),
        modelType: props.typeData,
        pageSize: 20,
        page: 1,
      },
      total: 0,
      title: '',
    })

    const handlAdd = () => {
      data.title = '新建联系人'
      data.addContactDialog.open(null, props.typeData)
    }

    // 获取列表
    const handlList = () => {
      api.terminal.CONTACT_LIST(data.queryParams).then((res) => {
        if (res.code === 0) {
          data.tableData = res.data.list || []
          data.total = res.data.total
        }
      })
    }

    // 编辑
    const handlEdit = (row) => {
      data.title = '编辑联系人'
      data.addContactDialog.open(row, props.typeData)
    }

    // 删除
    const handlDele = (id) => {
      currentInstance.proxy
        .alertBox({
          message: '删除数据后不可恢复,确定删除？',
        })
        .then(() => {
          api.terminal.CONTACT_DELETE(id).then((res) => {
            if (res.code) {
              currentInstance.proxy.customMessage({
                type: 'error',
                message: res.msg,
              })
            } else {
              handlList()
            }
          })
        })
        .catch((err) => {
          console.log(err)
        })
    }

    // 刷新列表
    const succsess = () => {
      handlList()
    }

    const aloneClick = (row, column) => {
      if (column.label === '操作' || column.label === '查看文件') return
    }

    const cellStyle = () => {
      return 'cursor:pointer;'
    }
    onMounted(() => {
      handlList()
    })

    return {
      ...toRefs(data),
      handlAdd,
      handlList,
      handlEdit,
      handlDele,
      succsess,
      aloneClick,
      cellStyle,
    }
  },
}
</script>

<style scoped>
/* @import url(); 引入css类 */
</style>
